package com.cqie.tes.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.tes.comm.R;
import com.cqie.tes.domain.RoleUser;
import com.cqie.tes.domain.SysUser;
import com.cqie.tes.domain.vo.sys.user.SysUserOptionVo;
import com.cqie.tes.mapper.EmployeeMapper;
import com.cqie.tes.mapper.SysUserMapper;
import com.cqie.tes.service.RoleUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cqie.tes.service.SysUserService;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    @Autowired
    RoleUserService roleUserService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 根据用户ID查询权限列表
     * */
    public List<Long> getRoleIdBySysUserId(Long id){
        if (id == null){
            return null;
        }
        List<Long> roleIdList = new ArrayList<>();

        // 根据ID查询用户角色关系
        QueryWrapper<RoleUser> roleUserQueryWrapper = new QueryWrapper<>();
        roleUserQueryWrapper.eq("userId", id);
        List<RoleUser> roleUserList = roleUserService.list(roleUserQueryWrapper);

        for (RoleUser r : roleUserList
        ) {
            if (!roleIdList.contains(r.getRoleId())){
                roleIdList.add(r.getRoleId());
            }
        }
        return roleIdList;
    }

    /**
     * 根据用户ID删除角色信息
     * */
    public void deleteRoleUserBySysUserId(Long id){
        QueryWrapper<RoleUser> roleUserQueryWrapper = new QueryWrapper<>();
        roleUserQueryWrapper.eq("userId", id);
        roleUserService.remove(roleUserQueryWrapper);
    }



    @Override
    public R optionList() {
        // 结果列表
        List<SysUserOptionVo> vos  =new ArrayList<>();
        // 一级菜单
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
//        QueryWrapper<Employee> emqw = new QueryWrapper<>();
//        List<Employee> employeeList = employeeMapper.selectList(emqw);
//        List<Long> ids = new ArrayList<>();
//        employeeList.forEach(employee -> {
//            if (!ids.contains(employee.getUserId())) {
//                ids.add(employee.getUserId());
//            }
//        });

        // 没有逻辑删除
        qw.eq("mark", 1);
        // 没有指定员工
        qw.orderByDesc("id");
//        if (!ids.isEmpty()){
//            qw.notIn("id",ids);
//        }

        List<SysUser> entityList = sysUserMapper.selectList(qw);

        entityList.forEach(entity->{
            SysUserOptionVo vo = new SysUserOptionVo();
            BeanUtils.copyProperties(entity,vo);
            System.out.println(entity);
            vos.add(vo);
        });
        return R.ok().message("查询成功").data("options", vos);
    }
}
